<?php
!defined('P_W') && exit('Forbidden');
require_once MODE . '/base/frontcontroller.php';
/**
 * 出租房列表页面
 */
class HireController extends FrontController {
	var $_layoutFile = 'hire';
	var $_baseUrl = 'index.php?m=house&q=hire';
	var $_houseHireHelper;
	var $_seopage;
	var $_defaultSeo;
	var $_seoname;
	var $_num = 20;
	function _before(){
		parent::_before();
		$this->_houseHireHelper = House::LoadClass('househirehelper', 'helper');
		$this->_viewer->current['hire'] = 'class="current"';
		return true;
	}
	function run() {
		$expend = array();	
		$condition = array();	
		$this->_viewer->url = $this->_baseUrl;
		$hview = House::LoadClass('houseview', 'utility');
		list($page, $keywords, $houseids, $act, $options) = $this->_gp(array('page','keywords','houseids','act', 'options'));
		$this->_viewer->act = !$act ? 'all' : $act;
		$this->_viewer->baseUrl = $hview->getBaseUrl($this->_baseUrl, $this->_viewer->act, $options, $keywords);
		
		$this->_viewer->isHouseManager = isHouseManager($this->_getGlobal('winduid'),'hire');
		$params = $hview->getParams($options);
		$this->_viewer->options = $hview->getHireOptionUrl($this->_baseUrl . '&act='.$act, $options);
		list($areatype, $platetype, $housetype, $pricetype, $roomtype, $hiretype, $sort) = array($params['areatype'], $params['platetype'],$params['housetype'], $params['pricetype'], $params['roomtype'], $params['hiretype'], $params['sort']);
		
		$this->_viewer->areatype = (int)$areatype;
		$this->_viewer->platetype = (int)$platetype;
		$this->_viewer->housetype = (int)$housetype;
		$this->_viewer->pricetype = (int)$pricetype;
		$this->_viewer->roomtype = (int)$roomtype;
		$this->_viewer->hiretype = (int)$hiretype;
		
		$this->_viewer->sort = $sort;
		$sort && list($sortField, $sortType) = explode(',',$sort);		
		$this->_viewer->sortlist = $this->_getSortList();
		($act == 'urgent') && $expend['isurgent'] = 1;
		($act == 'personal') && $expend['isbroker'] = 0;
		$hireService = House::LoadService('SecondHireService');
		$this->_viewer->area = $hireService->getFieldsByType('area');//所属区域
		$this->_viewer->plate = $hireService->getPlateByAreaId($this->_viewer->areatype);
		$this->_viewer->house = $hireService->getFieldsByType('property');//房屋类型区间	
		$this->_viewer->price = $hireService->getFieldsByType('rentprice');//租金区间
		$this->_viewer->room = $hireService->roomType;//居室
		$this->_viewer->hire = $hireService->hireType;//租房方式
		$arrayIndex = array('areatype'=>$this->_viewer->areatype, 'platetype'=>$this->_viewer->platetype, 'housetype'=>$this->_viewer->housetype, 'pricetype'=>$this->_viewer->pricetype, 'roomtype'=>$this->_viewer->roomtype,	'hiretype'=>$this->_viewer->hiretype);
		$this->_viewer->areatype && $condition['areatype'] = $this->_viewer->area[$this->_viewer->areatype];
		$this->_viewer->platetype && $condition['platetype'] = $this->_viewer->plate[$this->_viewer->platetype];
		$this->_viewer->pricetype && $condition['pricetype'] = $this->_viewer->price[$this->_viewer->pricetype];
		$this->_viewer->roomtype && $condition['roomtype'] = $this->_viewer->room[$this->_viewer->roomtype];
		$this->_viewer->housetype && $condition['housetype'] = $this->_viewer->house[$this->_viewer->housetype];
		$this->_viewer->hiretype && $condition['hiretype'] = $this->_viewer->hire[$this->_viewer->hiretype];		
		$this->_viewer->condition = $condition;
		($this->_viewer->act == 'all') && $this->_viewer->toplist = $hireService->getSecondInfos($hview->getTopHouseids('hire', $arrayIndex));
		$page = (intval($page) == 0) ? 1 : intval($page);
		list($this->_viewer->total, $this->_viewer->result) = $hireService->getDetailList($arrayIndex, $sortType, $sortField,$keywords, $page, $this->_num, $expend);
		if($this->_viewer->act == 'all' && !empty($this->_viewer->toplist)){
			foreach($this->_viewer->toplist as $value){
				$value['showTop'] = 1;
				array_unshift($this->_viewer->result, $value);				
			}
		}
		$this->_viewer->pages = numofpage($this->_viewer->total, $page, ceil($this->_viewer->total/ $this->_num), $this->_viewer->baseUrl);//分页信息
		$seo = $this->_houseHireHelper->setSeoParams('index');
		list($this->_seopage,$this->_defaultSeo,$this->_seoname) = $seo;
	}
	
	function ajax(){
		define('AJAX','1');
		list($part, $houseids) = $this->_gp(array('part','houseids'));
		if(in_array($part,array('report')) ){
			global $_G; 
			!$_G['allowreport'] && Error::showError('您没有使用举报的权限');
		}else{
			if (!isHouseManager($this->_getGlobal('winduid'),'hire')) Error::showError('您没有权限进入该页面',1); 
		}
		if(!$houseids){
			echo '未选择要操作的项!';
			ajax_footer();	
		}
		include(D_P.'data/bbscache/house_config.php');
		include(R_P.'require/credit.php');
		$this->_viewer->house_hire_rapidprice = $house_hire_rapidprice;
		$this->_viewer->house_hire_rapidcredit = $credit->cType[$house_hire_rapidcredit];
		
		$this->_viewer->house_hire_topprice = $house_hire_topprice;
		$this->_viewer->house_hire_topcredit = $credit->cType[$house_hire_topcredit];
		$act = array('urgent'=>$this->_baseUrl . '&a=urgent' . '&houseids=' . $houseids,
					'top'=>$this->_baseUrl . '&a=top'. '&houseids=' . $houseids,
					'delete'=>$this->_baseUrl . '&a=delete'. '&houseids=' . $houseids,
					'report'=>$this->_baseUrl . '&a=report'. '&houseids=' . $houseids,);		
		$this->_viewer->act = $act;		
		$this->_viewer->part = $part;
		$this->_setLayoutFile('ajax');
		$this->_setLayoutType('ajax');
	}
	
	function urgent(){
		if (!isHouseManager($this->_getGlobal('winduid'),'hire')) Error::showError('您没有权限进入该页面',1); 
		$this->_viewer->baseUrl = $this->_baseUrl;
		list($houseids, $days, $isurgent, $isback, $ismsg) = $this->_gp(array('houseids', 'days', 'isurgent' ,'isback', 'ismsg'));
		if(!$houseids){
			echo '数据错误!';
			ajax_footer();
		}		
		$houseids = explode(',', $houseids);
		$hireService = House::LoadService('SecondHireService');
		$ismsg = $isurgent ? 1 : $ismsg;
		if($hireService->setUrgent($houseids, $isurgent, $days, $isback, $ismsg)){;
			echo "操作成功!\treload";
			ajax_footer();
		}
	}
	
	function top(){
		if (!isHouseManager($this->_getGlobal('winduid'), 'hire')) Error::showError('您没有权限进入该页面',1); 
		$this->_viewer->baseUrl = $this->_baseUrl;		
		list($houseids, $days, $istop, $isback, $ismsg) = $this->_gp(array('houseids', 'days', 'istop' ,'isback', 'ismsg'));
		if(!$houseids){
			echo '数据错误!';
			ajax_footer();
		}		
		$houseids = explode(',', $houseids);
		$hireService = House::LoadService('SecondHireService');
		$ismsg = $istop ? 1 : $ismsg;
		if($hireService->setTop($houseids, $istop, $days, $isback, $ismsg)){;
			echo "操作成功!\treload";
			ajax_footer();
		}
	}
	
	function report(){
		global $winduid,$windid,$timestamp;
		list($houseids,$reason,$ifsendmsg) = $this->_gp(array('houseids','reason','ifsendmsg'));
		if(!trim($reason)){
			echo "举报内容不能为空!";
			ajax_footer();
		}
		$houseReportService =  $this->_houseHireHelper->getReportService();
		if($houseReportService->checkReport($houseids,'hire')){
			echo "此房源已被举报,已在处理中";
			ajax_footer();
		}
		$data = array(
			'uid'=>$winduid,
			'username'=>$windid,
			'reason'=>$reason,
			'handle'=>0,
			'type'=>1,
			'id'=>$houseids,
			'time'=>$timestamp
		);
		$houseReportService->add($data);
		echo "举报成功!\treload";
		ajax_footer();
	}
	
	function delete(){
		if (!isHouseManager($this->_getGlobal('winduid'), 'hire')) Error::showError('您没有权限进入该页面',1);
		list($houseids) = $this->_gp(array('houseids'));
		if(!$houseids){
			echo '数据错误!';
			ajax_footer();
		}		
		$houseids = explode(',', $houseids);		
		$hireService = House::LoadService('SecondHireService');
		if($hireService->delete($houseids)){;
			echo "删除成功!\treload";
			ajax_footer();
		}
	}
	
	function houseinfo(){
		list($houseid) = GP(array('houseid'));
		!$houseid && Showmsg("data_error");
		$houseHireModule = $this->_houseHireHelper->getHouseHireModule($houseid,1,1);
		$brokerHelper = House::LoadClass('brokerhelper', 'helper');
		(!$houseHireModule->subject || !$houseHireModule->username) && Showmsg("房源不存在");
		if($houseHireModule->state == 1){
			Error::showError("对不起，该房源已下架");
		}elseif($houseHireModule->state == 2){
			Error::showError("对不起，该房源已过期");
		}
		$hireService = $this->_houseHireHelper->getHouseHireService();
		$imageService = $this->_houseHireHelper->getSecondImageService();
		
		$browselog = str_replace(",$houseid,", ',', GetCookie('browsehirelog'));
		if ($browselog  && ($log = explode(',', trim($browselog, ',')))) {
			$this->_viewer->browseHireHouse = $hireService->getSecondInfos(array_slice($log, -6, 6));
		}
		$browselog .= ($browselog ? '' : ',') . $houseid . ',';
		substr_count($browselog, ',') > 11 && $browselog = preg_replace("/[\d]+\,/i", '', $browselog, 3);
		Cookie('browsehirelog', $browselog);
		
		$this->_viewer->lastUpdateHouse = $hireService->getBorkerHiresByTime($houseHireModule->uid,$houseid,5);
		$this->_viewer->similarHireHouse = $hireService->getSimilarList($houseid,$houseHireModule->areatype,$houseHireModule->price);
		$this->_viewer->secondImages = $imageService->getTopImages($houseid, 'hire', 4);
		$this->_viewer->brokerModule = $brokerHelper->getBrokerModule($houseHireModule->uid,1);
		$this->_viewer->ifbroker = ($this->_viewer->brokerModule->uid &&  $this->_viewer->brokerModule->ifactive) ? true : false;
		$this->_viewer->houseid = $houseid ;
		$this->_viewer->houseHireModule = $houseHireModule ;
		$this->_viewer->baseUrl = $this->_baseUrl;
		$this->_viewer->showonline = $this->_getGlobal('db_showonline');
		$this->_viewer->imgpath = $this->_getGlobal('imgpath'); 
		$this->_viewer->modeImagePath = IMG_PATH;
		$seo = $this->_houseHireHelper->setSeoParams('read',$houseHireModule->subject,$houseHireModule->areatype,$houseHireModule->village);
		list($this->_seopage,$this->_defaultSeo,$this->_seoname) = $seo;
		
	}
	
	function agent(){
		
	}
	
	function _getSortList(){
		return array(
			'updatetime,DESC'=>'按更新时间',
			'posttime,DESC'=>'按发布时间',
			'price,ASC'=>'按租金从低到高',
			'price,DESC'=>'按租金从高到低'
		);
	}
	
	function _after(){
		houseSeoSettings($this->_seopage,$this->_defaultSeo,$this->_seoname);
	}
	
}